<template>
	<div class="table-demo-container layout-padding">
		<div class="table-demo-padding layout-padding-view layout-padding-auto">
			<TableSearch :search="state.tableData.search" @search="onSearch" />
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" class="table" stripe>
				<el-table-column type="index" label="序号" width="80" />
				<el-table-column prop="id" label="实验设备编号" width="200" show-overflow-tooltip></el-table-column>
				<el-table-column prop="name" label="实验设备名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="count" label="数量" show-overflow-tooltip></el-table-column>
				<el-table-column prop="picture" label="图片" show-overflow-tooltip>
					<template #default="scope">
						<el-image :src="scope.row.picture" fit="fill" style="width: 100px; height: 100px"></el-image>
					</template>
				</el-table-column>
				<el-table-column prop="description" label="实验描述" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditRole('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				:current-page="state.tableData.param.pageNum"
				background
				:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</div>
		<RoleDialog ref="roleDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="equipmentsManagement">
import { defineAsyncComponent, reactive, ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { equipmentInfo, delEquipmentInfo, getEquipmentInfo } from '/@/api/equipment/management';

// 引入组件
const TableSearch = defineAsyncComponent(() => import('/@/views/equipments/management/search.vue'));
const RoleDialog = defineAsyncComponent(() => import('/@/views/equipments/management/dialog.vue'));

// 定义变量内容
const tableRef = ref<RefType>();
const roleDialogRef = ref();
const state = reactive({
	tableData: {
		// 列表数据（必传）
		data: [],
		// 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
		search: [
			{ label: '设备编号', prop: 'id', placeholder: '请输入想查询的设备编号', required: false, type: 'input' },
			{ label: '设备名称', prop: 'name', placeholder: '请输入想查询的设备名称', required: false, type: 'input' },
		],
		// 搜索参数（不用传，用于分页、搜索时传给后台的值，`getTableData` 中使用）
		param: {
			pageNum: 1,
			pageSize: 10,
		},
		loading: false,
		total: 0,
	},
});

// 初始化列表数据
const getTableData = () => {
	state.tableData.loading = true;
	state.tableData.data = [];
	equipmentInfo(state.tableData.param.pageNum)
		.then((res) => {
			if (res.code === '2000') {
				state.tableData.data = res.data.map((item: any) => {
					return {
						id: item.equipment_id,
						name: item.equipment_name,
						count: item.count,
						picture: item.picture,
						description: item.description,
					};
				});
				state.tableData.total = res.totalCount;
			} else {
				ElMessage.error('请求数据失败!');
			}
		})
		.catch((err) => {
			ElMessage.error('请求数据失败!' + err);
		});
	setTimeout(() => {
		state.tableData.loading = false;
	}, 500);
};
// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { ...data });
	const searchInfo = {
		Page: state.tableData.param.pageNum,
		equipment_id: data.id,
		equipment_name: data.name,
	};
	getEquipmentInfo(searchInfo)
		.then((res) => {
			if (res.code === '2000') {
				state.tableData.data = res.data.map((item: any) => {
					return {
						id: item.equipment_id,
						name: item.equipment_name,
						count: item.count,
						picture: item.picture,
						description: item.description,
					};
				});
				state.tableData.total = res.totalCount;
			} else {
				ElMessage.error('请求数据失败!');
			}
		})
		.catch((err) => {
			ElMessage.error('请求数据失败!' + err);
		});
	tableRef.value.pageReset();
};
// 打开修改实验设备弹窗
const onOpenEditRole = (type: string, row: Object) => {
	roleDialogRef.value.openDialog(type, row);
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 删除实验设备
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除班级：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			delEquipmentInfo({ equipment_id: row.id })
				.then((res) => {
					if (res.code === '2000') {
						ElMessage.success('删除成功');
						getTableData();
					}
				})
				.catch(() => {
					ElMessage.error('删除失败');
				});
		})
		.catch((err: any) => {
			ElMessage.error('删除失败' + err);
		});
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.table-demo-container {
	.table-demo-padding {
		padding: 15px;
		.table-demo {
			flex: 1;
			overflow: hidden;
		}
	}
}
</style>
